//
//  HNAudioView.m
//  BestLearning
//
//  Created by 星道 on 17/6/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNAudioView.h"
#import <AVFoundation/AVFoundation.h>
@implementation HNAudioView
{
    /** 音频播放器 */
    AVPlayer *_Player;
    NSString *_UrlStr;
    
    /** 图片全屏 */
    UIView *_BlackGroundView;
    UIImage *_Img;
}

#pragma mark - 音频
/** 弹窗 */
- (void)OpenWithUrl:(NSString *)UrlStr
{
    _UrlStr = UrlStr;
    if (_Player)
    {
        [self Pause];   //停止播放
    }
    else
    {
        [self Play];    //开始播放
    }
}


//开始播放
- (void)Play
{
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"是否播放" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"播放" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        /** 实例化播放放器 */
        _Player = [[AVPlayer alloc] init];
        AVPlayerItem *Item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:_UrlStr]];
        [_Player replaceCurrentItemWithPlayerItem:Item];
        [_Player play];
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:Alert animated:YES completion:nil];
    
}


/** 停止播放 */
- (void)Pause
{
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"是否停止播放" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"停止" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        [_Player pause];
        _Player = nil;
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:Alert animated:YES completion:nil];
}


#pragma mark - 图片
/** 图片全屏操作 */
- (void)OpenWithImg:(UIImage *)Img
{
    _Img = Img;
    
    //设置黑色背景
    [self BackgroundView];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_BlackGroundView];
    
    [_BlackGroundView addSubview:[self ConfigImg]];
    
    [self Open];
}



- (void)Open
{
    [UIView animateWithDuration:0.3 animations:^{
        _BlackGroundView.alpha = 1;
    }];
}

//关闭
- (void)Close
{
    [UIView animateWithDuration:0.3 animations:^{
        _BlackGroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [_BlackGroundView removeFromSuperview];
    }];
}


- (UIImageView *)ConfigImg
{
    //读取图片的宽高
    CGSize ImgSize = _Img.size;
    CGFloat Scale = ImgSize.width/ImgSize.height;
    CGFloat W = kScreenWidth;
    CGFloat H = kScreenWidth/Scale;
    
    UIImageView *ImgView = [[UIImageView alloc] init];
    ImgView.frame = CGRectMake(0, 0, W, H);
    ImgView.center = CGPointMake(kScreenWidth/2, kScreenHeight/2);
    ImgView.image = _Img;
    return ImgView;
}



//黑背景
- (void)BackgroundView
{
    _BlackGroundView = [[UIView alloc] init];
    _BlackGroundView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    _BlackGroundView.backgroundColor = [UIColor blackColor];
    _BlackGroundView.alpha = 0;
    [_BlackGroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Close)]];
}
@end
